Skip to content

Emit flat JSON log records without redundant text field#34

Merged
jdoss merged 1 commit intomasterfrom
fix/clean-json-logs
Apr 17, 2026
Merged

Emit flat JSON log records without redundant text field#34
jdoss merged 1 commit intomasterfrom
fix/clean-json-logs

Conversation

@jdoss
Copy link
Copy Markdown
Contributor

@jdoss jdoss commented Apr 17, 2026

Summary

Loguru's default serialize=True produces {"text": "...\n", "record": {...}} where the text field is a pre-formatted duplicate of the structured data with an embedded newline. Log aggregators then surface literal \n escapes in the JSON body, making entries harder to read and redundant with per-line framing.

Replace the serialize=True sink with a custom JSON sink that emits a flat record with just the useful fields: time, level, message, module, function, line, name, plus extra (from logger.bind()). No text field, no embedded newlines.

Before

{"text": "2026-04-17 05:24:29.387 | INFO | psi.serve:_handle_lookup:232 - lookup\n", "record": {"elapsed": {...}, "extra": {"event": "secret.lookup", ...}, "message": "lookup", "level": {"icon": "ℹ️", "name": "INFO", "no": 20}, "line": 232, ...}}

After

{"time": "2026-04-17T05:24:29.387741+00:00", "level": "INFO", "message": "lookup", "module": "serve", "function": "_handle_lookup", "line": 232, "name": "psi.serve", "extra": {"event": "secret.lookup", "secret_id": "...", "provider": "infisical", "outcome": "success", "source": "cache"}}

Test plan

  • pytest tests/test_logging.py tests/test_secret.py — all 22 tests pass; test_logging.py rewritten to exercise configure_logging(json_output=True) directly and assert on the flat structure.
  • ruff check / ty check — clean.
  • Deploy to test server, verify Victoria Logs entries come through flat with no \\n escapes in the body.

Loguru's default serialize=True produces {"text": "...\n", "record":
{...}} where the text field is a pre-formatted duplicate of the
structured data with an embedded newline. Log aggregators then
surface literal \n escapes in the JSON, making entries harder to
read and redundant with per-line framing.

Replace the serialize=True sink with a custom JSON sink that emits a
flat record: time, level, message, module, function, line, name, and
the bind()-supplied extra as a nested dict. No text field, no
embedded newlines.

Update test_logging.py to exercise configure_logging(json_output=True)
directly and assert on the flat structure.
@jdoss jdoss merged commit 606e85a into master Apr 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant